package hello;




import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStoreException;



public class HelloMIDlet extends MIDlet implements CommandListener {

     private Command exitCommand, ver, ok,jugar,guardarm, crearm, crears, regresar, abrirm, leerscript, abrirs,guardars ; // The exit command
    private Display display;     // The display for this MIDlet
    private Pantalla pantallainicial;
    private TextBox mapa;
    private TextBox script;
    private Mapa robotillo;
	String textoScript;
    public HelloMIDlet() {
        display = Display.getDisplay(this);

        exitCommand = new Command("Exit", Command.EXIT, 0);
        regresar = new Command("Atras", Command.BACK, 1);
        ok = new Command("Ok", Command.SCREEN, 2);
        jugar = new Command("Jugar", Command.SCREEN,1) ;
        guardarm = new Command("Guardar", Command.OK, 2);
        ver = new Command("Visualizar", Command.OK, 2);
        crearm = new Command("Crear mapa", Command.SCREEN, 1);
        crears = new Command("Crear script", Command.SCREEN, 1);
        abrirm = new Command("Abrir", Command.OK, 2);
        leerscript = new Command("Ejecutar", Command.OK, 2);
        abrirs = new Command("Abrir", Command.OK, 2);
        guardars = new Command("Guardar", Command.OK, 2);

        pantallainicial = new Pantalla();
        pantallainicial.addCommand(exitCommand);
        pantallainicial.addCommand(ok);
        pantallainicial.addCommand(jugar);
        pantallainicial.addCommand(crearm);
        pantallainicial.addCommand(crears);
        pantallainicial.setCommandListener(this);

		
		
        robotillo = new Mapa();

        robotillo.addCommand(regresar);
        robotillo.setCommandListener(this);

        mapa = new TextBox("Crear mapa", "", 154, 0);
        mapa.addCommand(exitCommand);
        mapa.addCommand(guardarm);
        mapa.addCommand(regresar);
        mapa.addCommand(ver);
        mapa.addCommand(abrirm);
        mapa.setCommandListener(this);


        script = new TextBox("Crear script", "", 200, 0);
        script.addCommand(exitCommand);
       // script.addCommand(guardars);
        script.addCommand(regresar);
        script.addCommand(leerscript);
      //  script.addCommand(abrirs);
        script.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(pantallainicial);
        new Thread(pantallainicial).start();
    }	
	
    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
        if (c == crearm) {
            display.setCurrent(mapa);
        }

        if (c == crears) {
            display.setCurrent(script);
        }
		if (c == leerscript) {
			textoScript = script.getString();
			robotillo = new Mapa();
			if(robotillo.checkScript(textoScript)){
				System.out.println("entro");
				robotillo.addCommand(regresar);
				robotillo.setCommandListener(this);
				display.setCurrent(robotillo);
				robotillo.mapaScript(textoScript);
			}
		}
        if (c == jugar) {
             robotillo = new Mapa();
             robotillo.addCommand(regresar);
             robotillo.setCommandListener(this);
			 display.setCurrent(robotillo);


                }
         if (c == regresar) {
            display.setCurrent(pantallainicial);
         }
    }

}
